<?php

/**
 * Utility
 * 
 * Library with static methods to support development and ease
 * up several iterations and cleanups.
 * 
 * @author Oliver Schwarz <oliver.schwarz@vaicon.de>
 * @package Core
 */
class Util
{

        /**
         * Sanitize a string
         * @param string $str String to be sanitized
         * @return string Sanitized string
         */
        public static function sanitize($str)
        {
                return addslashes($str);
        }

        /**
         * Show human readable debug output
         * @param mixed $dat Data to be printed out
         * @return void
         */
        public static function debug($dat)
        {
                printf('<pre>%s</pre>', print_r($dat, 1));
        }

        /**
         * Parse request
         * Parses the request and returns the request contents
         * as an object.
         */
        public static function parseRequest()
        {
                $ctrl = new stdClass;
                $ctrl->controller = 'home';
                $ctrl->action = 'list';
                $ctrl->item = '';
                if (isset($_SERVER['REDIRECT_URL'])):
                        $url = str_replace('/announceapp', '', $_SERVER['REDIRECT_URL']);
                        $tmp = explode('/', $url);
                        array_shift($tmp);
                        if (@trim($tmp[0]) != '') $ctrl->controller = $tmp[0];
                        if (@trim($tmp[1]) != '') $ctrl->action = $tmp[1]; 
                        if (@trim($tmp[2]) != '') $ctrl->item = $tmp[2];
                endif;
                return $ctrl;
        }

        /**
         * Go to a destination
         * 
         * Loads a destination (relative to the apps url) using
         * the php/http header directive.
         * 
         * @param string $dest
         * @return void
         */
        public static function go($dest)
        {
                if (!$dest{0} == '/') $dest = '/' . $dest;
                $red = sprintf("%s%s", APP_URL, $dest);
                header("Location: " . $red);
        }

        /**
         * Load a foreign controller
         * Sometimes you'll need to highjack another controller.
         * @param string $controller Controller's title (lowercase)
         * @return boolean true or false
         */
        public static function loadController($controller)
        {
                if (@require APP_DIR . '/app/controller/' . $controller . '.class.php'):
                        $classname = ucfirst($controller);
                        return new $classname;
                endif;
                return false;
        }

        /**
         * Calculate date from
         * @param int $unix_timestamp Timestamp to calculate from
         * @return string calculated date from
         * @todo Add support for smarty language translations
         */
        public static function calculateDateFrom($unix_timestamp)
        {

            $diff = mktime() - $unix_timestamp;
    
            if     ($diff < 60):
                    return sprintf('%d %s %s', $diff, $diff == 1 ? 'second' : 'seconds', 'ago');
            elseif ($diff < 3600):
                    return sprintf('%d %s %s', round($diff/60), round($diff/60) == 1 ? 'minute' : 'minutes', 'ago');
            elseif ($diff < 86400):
                    return sprintf('%d %s %s', round($diff/3600), round($diff/3600) == 1 ? 'hour' : 'hours', 'ago');
            elseif ($diff < 604800):
                    return sprintf('%d %s %s', round($diff/86400), round($diff/86400) == 1 ? 'day' : 'days', 'ago');
            else:
                    return 'Older than a week';
            endif;

        }

}

?>